// pages/mapPage/mapPage.js
import ajax from "../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      lat: null,
      lng: null
    },
    markers: [],
    pickerShow: false
  },
  pickerCancel () {
    this.setData({ pickerShow: false })
  },
  markertap(event) {
    console.log('markertap', event)
    this.setData({ pickerShow: true })
  },
  addMarkers() {
    const iconPath = '/image/map-icon.png'
    const entity = {
      iconPath,
      width: 30,
      height: 30,
      joinCluster: true,
      anchor: { x: .5, y: 1 },
      ariaLabel: '测试'
    }

    const markers = [
      {
        ...entity,
        id: 1,
        title: '测试1',
        latitude: 30.181031,
        longitude: 120.156104
      },
      {
        ...entity,
        id: 2,
        title: '测试2',
        latitude: 30.182031,
        longitude: 120.156104
      }
    ]
    this.setData({ markers })
    this.mapCtx.addMarkers({
      markers,
      clear: true,
      complete(res) {
        console.log('addMarkers', res)
      }
    })

  },
  /**
   * 获取定位授权
   */
  async getLocation() {
    const { formData } = this.data
    const res = await ajax.getLocationQQMap()
    formData.lat = res.location.lat
    formData.lng = res.location.lng
    this.setData({ formData: formData })
    this.addMarkers()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.data.mapContext = wx.createMapContext('mapId')
    this.mapCtx = wx.createMapContext('mapId')
    this.mapCtx.on('markerClusterClick', res => {
      console.log('markerClusterClick', res)
    })
    this.getLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})